import time
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from common.utils import chromeDriver


class PersonalCenter:
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://8.155.1.153/?s=user/index.html"
        self.driver = chromeDriver.driver
        self.driver.get(self.url)

    def BasicCheckElement(self):
        #头像
        element = self.driver.find_element(By.SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main.user-center > div.user-content > div > div.user-base.am-radius > div.user-base-left > div.user-avatar > img")
        assert element.is_displayed()
        #用户名
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main.user-center > div.user-content > div > div.user-base.am-radius > div.user-base-left > div.user-avatar > div > span")
        assert element.is_displayed()
        assert element.text == "209779"
        #修改头像
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main.user-center > div.user-content > div > div.user-base.am-radius > div.user-base-left > div.items > a:nth-child(1) > span").click()
        time.sleep(2)
        element = self.driver.find_element(By.CSS_SELECTOR, "#user-avatar-popup > div > div.am-popup-bd > form > div.submit-operation.am-margin-top-xs > div > input[type=file]")

        #取消
        self.driver.find_element(By.CSS_SELECTOR, "#user-avatar-popup > div > div.am-popup-bd > form > div.am-form-popup-submit > button.am-btn.am-btn-primary.am-radius.am-btn-xs.btn-loading-example").click()


    def AddressManagement(self):
        #点击我的地址
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main.user-center > div.user-content > div > div.user-base.am-radius > div.user-base-left > div.items > a:nth-child(3) > span").click()
        time.sleep(2)

        #点击新增
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > div.control > button").click()
        #等待并切换到iframe
        iframe = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(
                (By.CSS_SELECTOR, "iframe[id^='common-popup-iframe-']")
            )
        )#id^指定id的前缀去定位元素
        self.driver.switch_to.frame(iframe)
        # 在 iframe 内查找元素
        element = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR,
            "body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(1) > input"))
        )

        #清空姓名，电话，省市区，详细地址输入框
        chromeDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(1) > input"))
        chromeDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(3) > input"))
        chromeDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(3) > input"))
        chromeDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#form-address"))

        #输入姓名
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(1) > input").send_keys("111111")
        #输入电话
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(3) > input").send_keys("1111111111")
        #输入省市区
        #点击北京市 北京市 东城区
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage.am-form-success > div > div.am-cascader.max-width > div.am-cascader-suffix > div").click()
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage.am-form-success > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.province.am-cascader-menu > div > ul > li:nth-child(1) > span").click()
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage.am-form-success > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.city.am-cascader-menu.am-active > div > ul > li > span").click()
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage.am-form-success > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.county.am-cascader-menu.\30 .am-active > div > ul > li.am-cascader-node.am-active > span").click()
        #输入详细地址
        self.driver.find_element(By.CSS_SELECTOR, "#form-address").send_keys("111111111111")
        #点击保存
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-popup-submit > button.am-btn.am-btn-primary.am-radius.am-btn-xs.btn-loading-example").click()

        chromeDriver.GetScreenShot()



